{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:04:52.007328700Z",
     "start_time": "2024-09-19T11:04:51.930544700Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "        user_id  merchant_id  label\n0         34176         3906      0\n1         34176          121      0\n2         34176         4356      1\n3         34176         2217      0\n4        230784         4818      0\n...         ...          ...    ...\n260859   359807         4325      0\n260860   294527         3971      0\n260861   294527          152      0\n260862   294527         2537      0\n260863   229247         4140      0\n\n[260864 rows x 3 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230784</td>\n      <td>4818</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>260859</th>\n      <td>359807</td>\n      <td>4325</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>260860</th>\n      <td>294527</td>\n      <td>3971</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>260861</th>\n      <td>294527</td>\n      <td>152</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>260862</th>\n      <td>294527</td>\n      <td>2537</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>260863</th>\n      <td>229247</td>\n      <td>4140</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>260864 rows × 3 columns</p>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "train_df = pd.read_csv('train_format1.csv')\n",
    "train_df"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id             0\nmerchant_id         0\nprob           261477\ndtype: int64"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df = pd.read_csv('test_format1.csv')\n",
    "test_df.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:19:48.376415800Z",
     "start_time": "2024-09-19T11:19:48.331255900Z"
    }
   },
   "id": "7207fa6a02711f8b",
   "execution_count": 59
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id         0\nage_range    2217\ngender       6436\ndtype: int64"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = pd.read_csv('user_info_format1.csv')\n",
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:04:52.121533100Z",
     "start_time": "2024-09-19T11:04:52.042959800Z"
    }
   },
   "id": "7c505ffe7d7cbf66",
   "execution_count": 28
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "        user_id  merchant_id  prob  age_range  gender\n0         98688         1964   NaN        6.0     0.0\n1         98688         3645   NaN        6.0     0.0\n2        295296         3361   NaN        2.0     1.0\n3         33408           98   NaN        2.0     0.0\n4        230016         1742   NaN        5.0     1.0\n...         ...          ...   ...        ...     ...\n196873   293759         2108   NaN        3.0     1.0\n196874   228479         3473   NaN        6.0     0.0\n196875   228479         3111   NaN        6.0     0.0\n196876    97919         2341   NaN        8.0     1.0\n196877    97919         3971   NaN        8.0     1.0\n\n[196878 rows x 5 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>prob</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>98688</td>\n      <td>1964</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>98688</td>\n      <td>3645</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>295296</td>\n      <td>3361</td>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>33408</td>\n      <td>98</td>\n      <td>NaN</td>\n      <td>2.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>230016</td>\n      <td>1742</td>\n      <td>NaN</td>\n      <td>5.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>196873</th>\n      <td>293759</td>\n      <td>2108</td>\n      <td>NaN</td>\n      <td>3.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>196874</th>\n      <td>228479</td>\n      <td>3473</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>196875</th>\n      <td>228479</td>\n      <td>3111</td>\n      <td>NaN</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>196876</th>\n      <td>97919</td>\n      <td>2341</td>\n      <td>NaN</td>\n      <td>8.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>196877</th>\n      <td>97919</td>\n      <td>3971</td>\n      <td>NaN</td>\n      <td>8.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>196878 rows × 5 columns</p>\n</div>"
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df = pd.merge(test_df,user_info, on=\"user_id\")\n",
    "test_df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:20:28.091193200Z",
     "start_time": "2024-09-19T11:20:28.043769700Z"
    }
   },
   "id": "43624d939764922a",
   "execution_count": 61
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "          user_id  item_id  cat_id  seller_id  brand_id  time_stamp  \\\n0          328862   323294     833       2882    2661.0         829   \n1          328862   844400    1271       2882    2661.0         829   \n2          328862   575153    1271       2882    2661.0         829   \n3          328862   996875    1271       2882    2661.0         829   \n4          328862  1086186    1271       1253    1049.0         829   \n...           ...      ...     ...        ...       ...         ...   \n54925325   208016   107662     898       1346    7995.0        1110   \n54925326   208016  1058313     898       1346    7995.0        1110   \n54925327   208016   449814     898        983    7995.0        1110   \n54925328   208016   634856     898       1346    7995.0        1110   \n54925329   208016   272094     898       1346    7995.0        1111   \n\n          action_type  \n0                   0  \n1                   0  \n2                   0  \n3                   0  \n4                   0  \n...               ...  \n54925325            0  \n54925326            0  \n54925327            0  \n54925328            0  \n54925329            0  \n\n[54925330 rows x 7 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>seller_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>328862</td>\n      <td>323294</td>\n      <td>833</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>328862</td>\n      <td>844400</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>328862</td>\n      <td>575153</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>328862</td>\n      <td>996875</td>\n      <td>1271</td>\n      <td>2882</td>\n      <td>2661.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>328862</td>\n      <td>1086186</td>\n      <td>1271</td>\n      <td>1253</td>\n      <td>1049.0</td>\n      <td>829</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>54925325</th>\n      <td>208016</td>\n      <td>107662</td>\n      <td>898</td>\n      <td>1346</td>\n      <td>7995.0</td>\n      <td>1110</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>54925326</th>\n      <td>208016</td>\n      <td>1058313</td>\n      <td>898</td>\n      <td>1346</td>\n      <td>7995.0</td>\n      <td>1110</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>54925327</th>\n      <td>208016</td>\n      <td>449814</td>\n      <td>898</td>\n      <td>983</td>\n      <td>7995.0</td>\n      <td>1110</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>54925328</th>\n      <td>208016</td>\n      <td>634856</td>\n      <td>898</td>\n      <td>1346</td>\n      <td>7995.0</td>\n      <td>1110</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>54925329</th>\n      <td>208016</td>\n      <td>272094</td>\n      <td>898</td>\n      <td>1346</td>\n      <td>7995.0</td>\n      <td>1111</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>54925330 rows × 7 columns</p>\n</div>"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log = pd.read_csv('user_log_format1.csv')\n",
    "user_log"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:05:48.380591700Z",
     "start_time": "2024-09-19T11:05:34.152382100Z"
    }
   },
   "id": "c4694284f4b666e5",
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\86166\\AppData\\Local\\Temp\\ipykernel_12556\\3783705973.py:1: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['age_range'].replace(np.nan,0.0,inplace=True)\n",
      "C:\\Users\\86166\\AppData\\Local\\Temp\\ipykernel_12556\\3783705973.py:2: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['gender'].replace(np.nan,2.0,inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "   user_id  age_range  gender\n0   376517        6.0     1.0\n1   234512        5.0     0.0\n2   344532        5.0     0.0\n3   186135        5.0     0.0\n4    30230        5.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>376517</td>\n      <td>6.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>234512</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>344532</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>186135</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>30230</td>\n      <td>5.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info['age_range'].replace(np.nan,0.0,inplace=True)\n",
    "user_info['gender'].replace(np.nan,2.0,inplace=True)\n",
    "user_info = user_info[user_info['age_range'] != 0.0]\n",
    "user_info = user_info[user_info['gender'] != 2.0]\n",
    "user_info.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:06:31.758105900Z",
     "start_time": "2024-09-19T11:06:31.729784300Z"
    }
   },
   "id": "a2b32042d456dd49",
   "execution_count": 32
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\86166\\AppData\\Local\\Temp\\ipykernel_12556\\3555640233.py:1: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['age_range'].replace(np.nan,-1,inplace=True)\n",
      "C:\\Users\\86166\\AppData\\Local\\Temp\\ipykernel_12556\\3555640233.py:3: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_info['gender'].replace(np.nan,-1,inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "user_id      0\nage_range    0\ngender       0\ndtype: int64"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info['age_range'].replace(np.nan,-1,inplace=True)\n",
    "# 用户性别。0表示女性，1表示男性，2和NULL表示未知\n",
    "user_info['gender'].replace(np.nan,-1,inplace=True)\n",
    "user_info.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:06:35.585751Z",
     "start_time": "2024-09-19T11:06:35.570444Z"
    }
   },
   "id": "d9674d4df4417bfa",
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\86166\\AppData\\Local\\Temp\\ipykernel_12556\\3003646163.py:2: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  user_log['brand_id'].replace(np.nan,-1,inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "user_id        0\nitem_id        0\ncat_id         0\nseller_id      0\nbrand_id       0\ntime_stamp     0\naction_type    0\ndtype: int64"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 商品品牌的唯一编码\n",
    "user_log['brand_id'].replace(np.nan,-1,inplace=True)\n",
    "user_log = user_log[user_log['brand_id'] != -1]\n",
    "user_log.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:06:40.161816300Z",
     "start_time": "2024-09-19T11:06:38.808158500Z"
    }
   },
   "id": "9c18c555995c65dd",
   "execution_count": 34
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender\n0    34176         3906      0        6.0     0.0\n1    34176          121      0        6.0     0.0\n2    34176         4356      1        6.0     0.0\n3    34176         2217      0        6.0     0.0\n4   362112         2618      0        4.0     1.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>121</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>4356</td>\n      <td>1</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>2217</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>362112</td>\n      <td>2618</td>\n      <td>0</td>\n      <td>4.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 年龄性别特征\n",
    "train_df = pd.merge(train_df,user_info,on=\"user_id\")\n",
    "train_df.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:06:42.525358900Z",
     "start_time": "2024-09-19T11:06:42.481856700Z"
    }
   },
   "id": "b315b3b3d363add4",
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\86166\\AppData\\Local\\Temp\\ipykernel_12556\\1364353482.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  user_log.rename(columns={\"seller_id\":\"merchant_id\"},inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": "user_id        0\nitem_id        0\ncat_id         0\nmerchant_id    0\nbrand_id       0\ntime_stamp     0\naction_type    0\ndtype: int64"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.rename(columns={\"seller_id\":\"merchant_id\"},inplace=True)\n",
    "user_log.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:06:45.690568Z",
     "start_time": "2024-09-19T11:06:45.372470400Z"
    }
   },
   "id": "e5e80897d8f59807",
   "execution_count": 36
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   user_id  merchant_id  label  age_range  gender   item_id  cat_id  brand_id  \\\n0    34176         3906      0        6.0     0.0  757713.0   821.0    6268.0   \n1    34176         3906      0        6.0     0.0  757713.0   821.0    6268.0   \n2    34176         3906      0        6.0     0.0  757713.0   821.0    6268.0   \n3    34176         3906      0        6.0     0.0  718096.0  1142.0    6268.0   \n4    34176         3906      0        6.0     0.0  757713.0   821.0    6268.0   \n\n   time_stamp  action_type  \n0      1110.0          0.0  \n1      1110.0          0.0  \n2      1110.0          0.0  \n3      1031.0          3.0  \n4      1031.0          3.0  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>merchant_id</th>\n      <th>label</th>\n      <th>age_range</th>\n      <th>gender</th>\n      <th>item_id</th>\n      <th>cat_id</th>\n      <th>brand_id</th>\n      <th>time_stamp</th>\n      <th>action_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713.0</td>\n      <td>821.0</td>\n      <td>6268.0</td>\n      <td>1110.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713.0</td>\n      <td>821.0</td>\n      <td>6268.0</td>\n      <td>1110.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713.0</td>\n      <td>821.0</td>\n      <td>6268.0</td>\n      <td>1110.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>718096.0</td>\n      <td>1142.0</td>\n      <td>6268.0</td>\n      <td>1031.0</td>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>34176</td>\n      <td>3906</td>\n      <td>0</td>\n      <td>6.0</td>\n      <td>0.0</td>\n      <td>757713.0</td>\n      <td>821.0</td>\n      <td>6268.0</td>\n      <td>1031.0</td>\n      <td>3.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df = pd.merge(train_df,user_log,on=[\"user_id\",\"merchant_id\"],how=\"left\")\n",
    "train_df.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:06:54.466727800Z",
     "start_time": "2024-09-19T11:06:49.927091600Z"
    }
   },
   "id": "3ff6b7feb5a70246",
   "execution_count": 37
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "X = train_df.drop(['user_id','merchant_id','label'],axis=1)\n",
    "y = train_df['label']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:06:56.876879800Z",
     "start_time": "2024-09-19T11:06:56.681743900Z"
    }
   },
   "id": "b9fbdc23e935a73b",
   "execution_count": 38
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:07:02.432843500Z",
     "start_time": "2024-09-19T11:07:01.236365100Z"
    }
   },
   "id": "1d15ba6d205653d5",
   "execution_count": 39
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 0, 0, ..., 0, 0, 0], dtype=int64)"
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "\n",
    "# todo 决策树分类\n",
    "dt_model = DecisionTreeClassifier(splitter=\"best\") # todo splitter也是用来控制决策树中的随机选项的，有两种输入值，输入”best\"，决策树在分枝时虽然随机，但是还是会优先选择更重要的特征进行分枝（重要性可以通过属性feature_importances_查看），输入“random\"，决策树在分枝时会更加随机，树会因为含有更多的不必要信息而更深更大，并因这些不必要信息而降低对训练集的拟合。\n",
    "dt_model.fit(X_train, y_train)\n",
    "\n",
    "y_pred = dt_model.predict(X_test)\n",
    "y_pred"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T12:20:43.474013200Z",
     "start_time": "2024-09-19T12:20:36.770216700Z"
    }
   },
   "id": "2b4f8c2767da205",
   "execution_count": 96
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7217791279704204\n"
     ]
    }
   ],
   "source": [
    "from sklearn import metrics as mcs\n",
    "\n",
    "roc = mcs.roc_auc_score(y_test,y_pred)\n",
    "accuracy = mcs.accuracy_score(y_test,y_pred)\n",
    "print(roc)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T12:20:46.036909100Z",
     "start_time": "2024-09-19T12:20:45.915891600Z"
    }
   },
   "id": "a8b4c0ff3ff21261",
   "execution_count": 97
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "user_id             0\nmerchant_id         0\nprob           196878\nage_range           0\ngender              0\ndtype: int64"
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df.isnull().sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T11:52:24.069922300Z",
     "start_time": "2024-09-19T11:52:24.064078200Z"
    }
   },
   "id": "8e2bf4c79e96d9d8",
   "execution_count": 89
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# X1 = test_df.drop(['user_id','merchant_id','prob'],axis = 1)\n",
    "# dt_model.fit(X1)\n",
    "# tree= dt_model.predict_proba(X1)[:,1]\n",
    "# # test_df['tree_prob']\n",
    "# tree"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-19T12:28:58.059292100Z",
     "start_time": "2024-09-19T12:28:58.038353200Z"
    }
   },
   "id": "338e4a513f0dd5aa",
   "execution_count": 98
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
